package com.ruoyi.project.data.service.impl;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.project.data.domain.TbTransportDriverCar;
import com.ruoyi.project.data.domain.WeightRecord;
import com.ruoyi.project.data.mapper.AbnormalMapper;
import com.ruoyi.project.data.mapper.TbTransportDriverCarMapper;
import com.ruoyi.project.data.service.AbnormalService;
import com.ruoyi.project.system.domain.SysConfig;
import com.ruoyi.project.system.mapper.SysConfigMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @Description 异常信息
 * @params  * @param null
 * @ return
 * @Author hh
 * Date create by 2020/4/5 11:19
 */
@Service
public class AbnormalServiceImpl  implements AbnormalService {


    @Autowired
    private AbnormalMapper abnormalMapper;
    @Autowired
    private TbTransportDriverCarMapper tbTransportDriverCarMapper;
    @Autowired
    private SysConfigMapper sysConfigMapper;

    /**
     * 获取称重异常数据
     * @param param
     * @return
     */
    @Override
    public List<Map<String, Object>> selectCzYcDataList(Map<String, Object> param) {
        List<Map<String, Object>> list = abnormalMapper.selectCzYcDataList2(param);
        if(list != null && list.size() > 0){
            for(Map<String, Object> map : list){
                // 车牌
                if(map.get("plantNoG") != null) {
                    if (map.get("plantNoG").equals(map.get("plantNo"))) {
                        map.put("plantNoG","无");
                    }
                    if (map.get("plantNoG") .equals("无牌车")){
                        map.put("plantNoG","未识别");
                    }
                    if (map.get("plantNoG") .equals("无车牌")){
                        map.put("plantNoG","未识别");
                    }
                }else{
                    map.put("plantNoG","无");
                }
               /** // 获取标准车轴
                // 根据运单号获取车辆信息
                map.put("axleSource","");
                map.put("axleSourceG","");
                int axle = 0;
                Map<String, Object> carAxleNum = tbTransportDriverCarMapper.getCarAxleNum(map.get("goodsCode").toString());
                if (carAxleNum != null && StringUtils.isNotBlank(carAxleNum.get("carAxleNums").toString())){
                    axle = axle + Integer.parseInt(carAxleNum.get("carAxleNums").toString());
                    if (StringUtils.isNotBlank(carAxleNum.get("source").toString()) && "1".equals(carAxleNum.get("source").toString())){
                        map.put("axleSource","来源于识别");
                    }
                }

                if (carAxleNum != null && StringUtils.isNotBlank(carAxleNum.get("carAxleNumsG").toString())){
                    axle = axle + Integer.parseInt(carAxleNum.get("carAxleNumsG").toString());
                    if (StringUtils.isNotBlank(carAxleNum.get("sourceG").toString()) && "1".equals(carAxleNum.get("sourceG").toString())){
                        map.put("axleSourceG","挂车来源于识别");
                    }
                    map.put("plantNoG",carAxleNum.get("carNoG"));
                }
                // 标准轴数
                if (axle >6){
                    axle =6;
                }
                map.put("axleNum",axle);

                // 超限
                if (map.get("isCxyc").equals("1")){
                    Double maxLimitWeight = tbTransportDriverCarMapper.getMaxLimitWeight(String.valueOf(axle));
                    map.put("czdesc",axle+"轴超"+maxLimitWeight.intValue()+"吨");
                }else if (map.get("isCxyc").equals("2")){
                    map.put("czdesc","无法识别行驶证信息");
                }else {
                    if (map.get("isCzyc").equals("1")){
                        map.put("czdesc","称重超核定总质量");
                    }else if (map.get("isCzyc").equals("2")){
                        map.put("czdesc","无法识别行驶证信息");
                    }
                }
                if (map.get("plantNoG").toString().contains("挂")){
                    TbTransportDriverCar tbTransportDriverCar = new TbTransportDriverCar();
                    tbTransportDriverCar.setCarLicenceNumber(map.get("plantNoG").toString());
                    List<TbTransportDriverCar> driverCars = tbTransportDriverCarMapper.selectTbTransportDriverCarList(tbTransportDriverCar);
                    if (driverCars == null && driverCars.size() <=0){
                        map.put("czdesc","该挂车尚未认证");
                    }

                    if (carAxleNum != null && StringUtils.isNotBlank(carAxleNum.get("carNoG").toString())
                            && "无".equals(carAxleNum.get("carNoG").toString()) && !carAxleNum.get("carNoG").equals(map.get("plantNoG"))){
                        map.put("czdesc","该挂车未与运单绑定");
                    }
                }*/
                Map<String, Object> carAxleNum = tbTransportDriverCarMapper.getCarAxleNum(map.get("goodsCode").toString());
                if (carAxleNum != null && StringUtils.isNotBlank(carAxleNum.get("carNoG").toString())){
                    map.put("plantNoG",carAxleNum.get("carNoG"));
                }
                if (map.get("plantNoG").equals("未识别")){
                    map.put("czdesc","该挂车未识别到车牌号");
                }
            }
        }
        return  list;
    }


    /**
     * 翻新称重数据
     * @param param
     * @return
     */
    public void updateRecord(Map<String, Object> param) {
        List<Map<String, Object>> list = abnormalMapper.selectCzYcDataList3(param);
        if(list != null && list.size() > 0){
            for(Map<String, Object> map : list){

//                if(map.get("plantNoG") != null) {
//                    if (map.get("plantNoG").equals(map.get("plantNo"))) {
//                        map.put("plantNoG","无");
//                    }
//                    if (map.get("plantNoG") .equals("无牌车")){
//                        map.put("plantNoG","未识别到车牌号码");
//                    }
//                    if (map.get("plantNoG") .equals("无车牌")){
//                        map.put("plantNoG","未识别到车牌号码");
//                    }
//                }else{
//                    map.put("plantNoG","无");
//                }

                Double grossWeight = Double.parseDouble(map.get("recordWeight").toString());
                Double czWeight = 0.0;
                if (StringUtils.isNotBlank(map.get("czWeight").toString())){
                    czWeight = Double.parseDouble(map.get("czWeight").toString());
                }
                int axle = 0;
                // 超限
                Map<String, Object> carAxleNum = tbTransportDriverCarMapper.getCarAxleNum(map.get("goodsCode").toString());
                if (carAxleNum != null && StringUtils.isNotBlank(carAxleNum.get("carAxleNums").toString())){
                    axle = axle + Integer.parseInt(carAxleNum.get("carAxleNums").toString());
                }
                // 挂车轴数
                if (carAxleNum != null && StringUtils.isNotBlank(carAxleNum.get("carAxleNumsG").toString())){
                    axle = axle + Integer.parseInt(carAxleNum.get("carAxleNumsG").toString());
                    map.put("plantNoG",carAxleNum.get("carNoG"));
                }

                map.put("isCxyc","0");
                map.put("isCpyc","0");
                map.put("czDesc"," ");
                if (axle > 0){
                    if ( axle >6){
                        axle =6;
                    }
                    Double maxLimitWeight = tbTransportDriverCarMapper.getMaxLimitWeight(String.valueOf(axle));
                    if(grossWeight > maxLimitWeight * 1000){
                        map.put("isCxyc","1");
                        map.put("czDesc",axle+"轴超"+maxLimitWeight.intValue()+"吨");
                    }
                }
                map.put("axle",axle);
                if (map.get("isCxyc").equals("1")){
                    if (grossWeight <= czWeight){
                        map.put("isCxyc","0");
                        map.put("czDesc","按轴数已超载，但按总质量未超载");
                    }
                }

                if (map.get("plantNoG").toString().contains("挂")){
                    TbTransportDriverCar tbTransportDriverCar = new TbTransportDriverCar();
                    tbTransportDriverCar.setCarLicenceNumber(map.get("plantNoG").toString());
                    List<TbTransportDriverCar> driverCars = tbTransportDriverCarMapper.selectTbTransportDriverCarList(tbTransportDriverCar);
                    if (driverCars == null && driverCars.size() <=0){
                        map.put("isCxyc","0");
                        map.put("isCpyc","1");
                        map.put("cpycDesc","该挂车尚未认证");
                    }

                    if (carAxleNum != null && StringUtils.isNotBlank(carAxleNum.get("carNoG").toString())
                            && "无".equals(carAxleNum.get("carNoG").toString()) && !carAxleNum.get("carNoG").equals(map.get("plantNoG"))){
                        map.put("isCxyc","0");
                        map.put("isCpyc","1");
                        map.put("cpycDesc","该挂车未与运单绑定");
                    }
                }
                if (map.get("plantNoG").equals("无牌车") || map.get("plantNoG").equals("无车牌")){
                    map.put("isCxyc","0");
                    map.put("isCpyc","1");
                    map.put("cpycDesc","未识别到车牌号码");
                }

                abnormalMapper.updateRecord(map);
            }
        }
    }


    /**
     * 车牌异常
     * @param param
     * @return
     */
    @Override
    public List<Map<String, Object>> selectCpYcDataList(Map<String, Object> param) {
        return abnormalMapper.selectCpYcDataList2(param);
    }

    /**
     * 运单异常
     * @param param
     * @return
     */
    @Override
    public List<Map<String, Object>> selectYdYcDataList(Map<String, Object> param) {
        return abnormalMapper.selectYdYcDataList2(param);
    }

    /**
     * 无称重记录出场
     * @param param
     * @return
     */
    @Override
    public List<Map<String, Object>> selectWczjlccDataList(Map<String, Object> param) {
        return abnormalMapper.selectYdYcDataList3(param);
    }

    @Override
    public SysConfig getPercentage(){
        return sysConfigMapper.checkConfigKeyUnique("weight_all_waybill");
    }

    @Override
    public List<Map<String, Object>> selectNoWaybillWeightRecordList(WeightRecord weightRecord) {
        return abnormalMapper.selectNoWaybillWeightRecordList(weightRecord);
    }

    /**
     * 24小时无称重记录
     * @param param
     * @return
     */
    @Override
    public List<Map<String, Object>> selectWczjlDataList(Map<String, Object> param) {
        return abnormalMapper.selectWczjlDataList(param);
    }

    /**
     * 超载出场
     * @param param
     * @return
     */
    @Override
    public List<Map<String, Object>> selectCzccDataList(Map<String, Object> param) {
        return abnormalMapper.selectCzccDataList(param);
    }

    /**
     *  无称重记录
     * @param param
     * @return
     */
    @Override
    public List<Map<String, Object>> selectNoRecordabnormal(Map<String, Object> param) {
        return abnormalMapper.selectNoRecordabnormal(param);
    }


}
